package cn.lu.joke.common;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import net.lucode.hackware.magicindicator.abs.IPagerNavigator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.ArrayList;
import java.util.List;

public class CircleNavigator extends View implements IPagerNavigator {
    private int mRadius;
    private int mStrokeWidth;
    private int mCircleSpacing;
    private int mCurrentIndex;
    private int mTotalCount;
    private Interpolator mStartInterpolator = new LinearInterpolator();
    private Paint mPaint = new Paint(1);
    private Paint mPaint2 = new Paint(1);
    private List<PointF> mCirclePoints = new ArrayList();
    private float mIndicatorX;
    private boolean mTouchable;
    private net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator.OnCircleClickListener mCircleClickListener;
    private float mDownX;
    private float mDownY;
    private int mTouchSlop;
    private boolean mFollowTouch = true;

    public CircleNavigator(Context context) {
        super(context);
        this.init(context);
    }

    private void init(Context context) {
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mRadius = UIUtil.dip2px(context, 3.0D);
        this.mCircleSpacing = UIUtil.dip2px(context, 8.0D);
        this.mStrokeWidth = UIUtil.dip2px(context, 1.0D);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint2.setStyle(Paint.Style.FILL);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension(this.measureWidth(widthMeasureSpec), this.measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int result = 0;
        switch(mode) {
            case -2147483648:
            case 0:
                result = this.mTotalCount * this.mRadius * 2 + (this.mTotalCount - 1) * this.mCircleSpacing + this.getPaddingLeft() + this.getPaddingRight() + this.mStrokeWidth * 2;
                break;
            case 1073741824:
                result = width;
        }

        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int result = 0;
        switch(mode) {
            case -2147483648:
            case 0:
                result = this.mRadius * 2 + this.mStrokeWidth * 2 + this.getPaddingTop() + this.getPaddingBottom();
                break;
            case 1073741824:
                result = height;
        }

        return result;
    }

    protected void onDraw(Canvas canvas) {
        this.drawCircles(canvas);
        this.drawIndicator(canvas);
    }

    private void drawCircles(Canvas canvas) {
        //this.mPaint.setStrokeWidth((float)this.mStrokeWidth);
        int i = 0;

        for(int j = this.mCirclePoints.size(); i < j; ++i) {
            PointF pointF = this.mCirclePoints.get(i);
            canvas.drawCircle(pointF.x, pointF.y, (float)this.mRadius, this.mPaint);
        }

    }

    private void drawIndicator(Canvas canvas) {
        if (this.mCirclePoints.size() > 0) {
            canvas.drawCircle(this.mIndicatorX, (float)((int)((float)this.getHeight() / 2.0F + 0.5F)), (float)this.mRadius, this.mPaint2);
        }

    }

    private void prepareCirclePoints() {
        this.mCirclePoints.clear();
        if (this.mTotalCount > 0) {
            int y = (int)((float)this.getHeight() / 2.0F + 0.5F);
            int centerSpacing = this.mRadius * 2 + this.mCircleSpacing;
            int startX = this.mRadius + (int)((float)this.mStrokeWidth / 2.0F + 0.5F) + this.getPaddingLeft();

            for(int i = 0; i < this.mTotalCount; ++i) {
                PointF pointF = new PointF((float)startX, (float)y);
                this.mCirclePoints.add(pointF);
                startX += centerSpacing;
            }

            this.mIndicatorX = ((PointF)this.mCirclePoints.get(this.mCurrentIndex)).x;
        }

    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (this.mFollowTouch) {
            if (this.mCirclePoints.isEmpty()) {
                return;
            }

            int currentPosition = Math.min(this.mCirclePoints.size() - 1, position);
            int nextPosition = Math.min(this.mCirclePoints.size() - 1, position + 1);
            PointF current = (PointF)this.mCirclePoints.get(currentPosition);
            PointF next = (PointF)this.mCirclePoints.get(nextPosition);
            this.mIndicatorX = current.x + (next.x - current.x) * this.mStartInterpolator.getInterpolation(positionOffset);
            this.invalidate();
        }

    }

    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch(event.getAction()) {
            case 0:
                if (this.mTouchable) {
                    this.mDownX = x;
                    this.mDownY = y;
                    return true;
                }
                break;
            case 1:
                if (this.mCircleClickListener != null && Math.abs(x - this.mDownX) <= (float)this.mTouchSlop && Math.abs(y - this.mDownY) <= (float)this.mTouchSlop) {
                    float max = 3.4028235E38F;
                    int index = 0;

                    for(int i = 0; i < this.mCirclePoints.size(); ++i) {
                        PointF pointF = (PointF)this.mCirclePoints.get(i);
                        float offset = Math.abs(pointF.x - x);
                        if (offset < max) {
                            max = offset;
                            index = i;
                        }
                    }

                    this.mCircleClickListener.onClick(index);
                }
        }

        return super.onTouchEvent(event);
    }

    public void onPageSelected(int position) {
        this.mCurrentIndex = position;
        if (!this.mFollowTouch) {
            this.mIndicatorX = ((PointF)this.mCirclePoints.get(this.mCurrentIndex)).x;
            this.invalidate();
        }

    }

    public void onPageScrollStateChanged(int state) {
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        this.prepareCirclePoints();
    }

    public void onAttachToMagicIndicator() {
    }

    public void notifyDataSetChanged() {
        this.prepareCirclePoints();
        this.invalidate();
    }

    public void onDetachFromMagicIndicator() {
    }

    public int getRadius() {
        return this.mRadius;
    }

    public void setRadius(int radius) {
        this.mRadius = radius;
        this.prepareCirclePoints();
        this.invalidate();
    }

    public void setCircleColor(int circleColor) {
        mPaint.setColor(circleColor);
    }

    public void setSelectedColor(int selectColor) {
        mPaint2.setColor(selectColor);
    }

    public int getStrokeWidth() {
        return this.mStrokeWidth;
    }

    public void setStrokeWidth(int strokeWidth) {
        this.mStrokeWidth = strokeWidth;
        this.invalidate();
    }

    public int getCircleSpacing() {
        return this.mCircleSpacing;
    }

    public void setCircleSpacing(int circleSpacing) {
        this.mCircleSpacing = circleSpacing;
        this.prepareCirclePoints();
        this.invalidate();
    }

    public Interpolator getStartInterpolator() {
        return this.mStartInterpolator;
    }

    public void setStartInterpolator(Interpolator startInterpolator) {
        this.mStartInterpolator = startInterpolator;
        if (this.mStartInterpolator == null) {
            this.mStartInterpolator = new LinearInterpolator();
        }

    }

    public int getCircleCount() {
        return this.mTotalCount;
    }

    public void setCircleCount(int count) {
        this.mTotalCount = count;
    }

    public boolean isTouchable() {
        return this.mTouchable;
    }

    public void setTouchable(boolean touchable) {
        this.mTouchable = touchable;
    }

    public boolean isFollowTouch() {
        return this.mFollowTouch;
    }

    public void setFollowTouch(boolean followTouch) {
        this.mFollowTouch = followTouch;
    }

    public net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator.OnCircleClickListener getCircleClickListener() {
        return this.mCircleClickListener;
    }

    public void setCircleClickListener(net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator.OnCircleClickListener circleClickListener) {
        if (!this.mTouchable) {
            this.mTouchable = true;
        }

        this.mCircleClickListener = circleClickListener;
    }

    public interface OnCircleClickListener {
        void onClick(int var1);
    }
}
